package www.ym.com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.ym.com.entity.Consum;
import www.ym.com.mapper.ConsumMapper;
import www.ym.com.service.ConsumService;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class ConsumServiceImpl extends ServiceImpl<ConsumMapper, Consum> implements ConsumService {

    @Autowired
    private ConsumMapper consumMapper;

    @Override
    public Page<Consum> consunPage(JSONObject jsonObject) {
        //当前页数
        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));

        //每页多少条数据
        Long pageSize = Long.valueOf(String.valueOf(jsonObject.get("pageSize")));

        //消费人
        String name = String.valueOf(jsonObject.get("name"));

        //消费类型
        String type = String.valueOf(jsonObject.get("type"));


        String startTime = String.valueOf(jsonObject.get("startTime"));
        String endTime = String.valueOf(jsonObject.get("endTime"));
        log.info("startTime" + startTime);
        log.info("endTime" + endTime);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //消费时间开始
        String startTime1 = "";
        //消费时间结束
        String endTime1 = "";

        if (!startTime.isEmpty()) {
            startTime1 = dateFormat.format(new Date(Long.valueOf(startTime)));
        }
        if (!endTime.isEmpty()) {
            endTime1 = dateFormat.format(new Date(Long.valueOf(endTime)));
        }

        LambdaQueryWrapper<Consum> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(name != "", Consum::getName, name).eq(type != "", Consum::getType, type)
                .ge(startTime!="", Consum::getConsumTime, startTime1)
                .le(endTime!="", Consum::getConsumTime, endTime1).orderByDesc(Consum::getConsumTime);

        Page<Consum> url = consumMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return url;
    }
}
